// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: daniel

#include "coin2/exchange/risk/risk_manager.h"

#include <algorithm>

#include "coin2/base/time.h"
#include "coin2/exchange/base/symbology/product.h"
#include "coin2/exchange/order/risk_manager.pb.h"

namespace coin2::exchange::risk {

RiskManager::RiskManager(const RiskManagerConfig& config) : config_{config} {
  int64_t window = config.rest_error_window_size_sec() * 1'000'000'000LL;
  rest_error_stats_ = std::make_unique<RestErrorStats>(window);
}

void RiskManager::HandleOrderUpdate(const exchange::base::order::OrderUpdate& upd) {
  int64_t  ts = upd.timestamp();
  UpdateTimestamp(ts);
  if (upd.update_type() == coin::proto::OG_REST_TIMEOUT_ERROR_TYPE) {
    rest_error_stats_->HandleRestError(ts);
  }
}

}  // namespace coin2::exchange::risk
